﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRB_0._1
{
    class Map
    {
        public List<VisualObject> objects;
        
        public Vector2 avatarStartPosition;
        public List<DuctTapeEnemy> enemies;


        public float[] ObjectsX;

        public float[] EnemiesX;
        public float[] EnemiesY;
        public Vector2 ButtonVector;
        public Rectangle Rect2;
        public bool ButtonON;
        public bool SpawnEnemies;
        public TimeSpan TimeButtonNotPressed;
        public TimeSpan TimeButtonCanBePressed;
        
        public Viewport VP;
        public ContentManager CT;

        public List<float> SpawnPlaces;
        public List<Button> Buttons;
        public float InitialwinRect;
        public float[] InitialSpawnPlaces;

        public Rectangle winRect;

        public Texture2D background;
        public Vector2 backgroundPosition;
        public Vector2 initialBackgroundPosition;

        public virtual void Initialize(ContentManager Content, Viewport Viewport)
        {
            objects = new List<VisualObject>();
            enemies = new List<DuctTapeEnemy>();
            SpawnPlaces = new List<float>();
            Buttons= new List<Button>();
            ButtonON = false;
            SpawnEnemies = true;
            TimeButtonNotPressed = TimeSpan.Zero;
            TimeButtonCanBePressed = TimeSpan.FromSeconds(1.0f);

            backgroundPosition = Vector2.Zero;
            initialBackgroundPosition = backgroundPosition;

            VP = Viewport;
            CT = Content;
        }
        public virtual void InitializeAfterChild()
        {
            ObjectsX = new float[objects.Count()];
            for (int i = 0; i < objects.Count(); i++)
            {
                ObjectsX[i] = objects[i].Position.X;
            }
            InitialSpawnPlaces = new float[SpawnPlaces.Count];
            for (int i = 0; i < SpawnPlaces.Count(); i++)
            {

            InitialSpawnPlaces[i] = SpawnPlaces[i];
            }
            InitialwinRect = winRect.X;
                
            EnemiesX = new float[enemies.Count()];
            EnemiesY = new float[enemies.Count()];
            for (int i = 0; i < enemies.Count(); i++)
            {
                EnemiesX[i] = enemies[i].Position.X;
                EnemiesY[i] = enemies[i].Position.Y;
            }
            //objects.CopyTo(objectsStartValues, 0);
        }

        public virtual void UpdateMap(float movespeed, bool forward)
        {
            //oppdatering av terreng, slik som heiser og fiender.
            
            if (!forward)
            {
                movespeed = movespeed * -1;
            }

            for (int i = 0; i < objects.Count(); i++)
            {
                objects[i].Position.X -= movespeed;
            }
            for (int i = 0; i < enemies.Count(); i++)
            {
                enemies[i].Position.X -= movespeed;
            }
            for (int i = 0; i < SpawnPlaces.Count(); i++)
            {
                SpawnPlaces[i] -= movespeed;
            }

            foreach (VisualObject dudd in objects)
            {
                if (dudd is AnimatedObject)
                {
                    dudd.Changeposition(movespeed);
                }
            }
            backgroundPosition.X -= (int)movespeed;
            winRect.X -= (int)movespeed;
        }
        public virtual void Update(GameTime gameTime)
        {
            foreach (DuctTapeEnemy enemy in enemies)
            {
                if (enemy.Active)
                    enemy.Update(gameTime);

                //foreach (Button stuff in objects) redundant
                //    stuff.Update(gameTime); Den under gjør dette bedre
            }
            foreach (VisualObject dudd in objects)
            {
                dudd.Update(gameTime);
            }
            if (ButtonON)
            {
                if (gameTime.TotalGameTime - TimeButtonNotPressed < TimeButtonCanBePressed)
                {
                    SpawnEnemies = false;
                }
                else
                {
                    SpawnEnemies = true;
                }
            }
            else
            {
                TimeButtonNotPressed = gameTime.TotalGameTime;
                SpawnEnemies = true;
            }
        }

        public virtual Game1.ENDING IsEnd(Avatar avatar, Viewport Viewport)
        {
            return Game1.ENDING.NOTYET;
        }
        public virtual void Restart(Avatar avatar)
        {
            
            for (int i = 0; i < objects.Count(); i++)
            {
                objects[i].Position.X = ObjectsX[i];
            }
            
            for (int i = 0; i < SpawnPlaces.Count(); i++)
            {
                SpawnPlaces[i] = InitialSpawnPlaces[i];
            }

            foreach (VisualObject dudd in objects)
            {
                dudd.Restart();
            }

            enemies.RemoveRange(0, enemies.Count());

            avatar.DyingAnimation.Active = true;
            avatar.Position = avatarStartPosition;
            winRect.X = (int)InitialwinRect;
            backgroundPosition = initialBackgroundPosition;

            //avatar.Position.Y = 0;
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, backgroundPosition, Color.White);
            foreach (VisualObject things in objects)
                things.Draw(spriteBatch);

            

            foreach (DuctTapeEnemy enemy in enemies)
            {
                if (enemy.Active)
                    enemy.Draw(spriteBatch);
            }
        }
        public void changeButtonState()
        {
            ButtonON = !ButtonON;
        }
        //public void CheckForCollisions(Avatar avatar)
        //{
            //foreach (VisualObject dudd in objects)
            //{
            //    if (dudd is Button)
            //    {
            //        //ButtonVector = dudd.GetPositions();

            //        Rect2 = new Rectangle((int)((Button)dudd).Position.X, (int)((Button)dudd).Position.Y, dudd.Width, dudd.Height);
            //        Button.State lastState = ((Button)dudd).currentState;
            //        if (Rect.Intersects(Rect2))
            //        {
            //            ((Button)dudd).currentState = Button.State.ON;
            //            ButtonON = true;
            //            if (lastState != Button.State.ON)
            //            {
            //                dudd.Position.Y += 10;
            //                avatar.Position.Y = MathHelper.Clamp(avatar.Position.Y, avatar.lastPosition.Y, thing.Position.Y - thing.Height / 2 - avatar.Height);
            //            }
            //        }
            //        else
            //        {
            //            dudd.ChangeState(Button.State.OFF);
            //            ButtonON = false;
            //            if (lastState != Button.State.OFF)
            //                dudd.Position.Y -= 10;
            //        }
            //    }
            //}
        //}
    }
}
